<template>
  <div>
    <div class="n-layout-page-header" style="margin: 0">
      <n-card :bordered="false" :header-style="{
                    padding: '20px',
                  }">
        <template #header>
          <text style="font-weight: 500;font-size: 18px;color: #3D3D3D;line-height: 25px;">入住订单</text>
        </template>
        <!--  这是由系统生成的CURD表格，你可以将此行注释改为表格的描述 -->
      </n-card>
    </div>
    <n-card :bordered="false" class="proCard"
            :content-style="{
                    padding: '0 20px 20px',
                  }">
      <BasicForm  ref="searchFormRef" labelWidth="200" @register="register" @submit="reloadTable" @reset="reloadTable" @keyup.enter="reloadTable">
        <template #statusSlot="{ model, field }">
          <n-input v-model:value="model[field]" />
        </template>
      </BasicForm>
      <BasicTable  ref="actionRef" :columns="columns" :request="loadDataTable" :actionColumn="actionColumn" :scroll-x="scrollX" :resizeHeightOffset="-10000">
        <template #tableTitle>

        </template>
      </BasicTable>
      <OrderView ref="orderViewRef" />
    </n-card>
    <Cancel ref="cancelRef" @reloadTable="reloadTable" />
  </div>
</template>

<script lang="ts" setup>
  import { h, reactive, ref, computed, onMounted, watch } from 'vue';
  import { BasicTable, TableAction } from '@/components/Table';
  import { BasicForm, useForm } from '@/components/Form/index';
  import { RoomList } from '@/api/pmsAppReservation';
  import { columns, schemas, loadOptions } from './model';
  import { adaTableScrollX } from '@/utils/hotgo';
  import OrderView from "../pmsAppReservation/view.vue";
  import {useUserStore} from "@/store/modules/user";
  import Cancel from './cancel.vue';

  const userStore = useUserStore();
  const actionRef = ref();
  const searchFormRef = ref<any>({});
  const orderViewRef = ref();
  const cancelRef = ref();

  const actionColumn = reactive({
    width: 130,
    title: '操作',
    key: 'action',
    fixed: 'right',
    render(record) {
      return h(TableAction as any, {
        style: 'button',
        actions: [
          {
            label: '详情',
            onClick: handleView.bind(null, record),
            // ifShow: () => {
            //   return record.orderSn;
            // },
          },
          {
            label: '取消',
            onClick: handleCancel.bind(null, record),
            ifShow: () => {
              return record.status !== "cancelled";
            },
            auth: ['/pmsAppReservation/cancel'],
            type: 'error'
          },
        ],
      });
    },
  });

  const scrollX = computed(() => {
    return adaTableScrollX(columns, actionColumn.width);
  });

  const [register, {}] = useForm({
    gridProps: { cols: '1 s:1 m:2 l:5 xl:5 2xl:5' },
    labelWidth: 120,
    schemas,
  });

  // 加载表格数据
  const loadDataTable = async (res) => {
    res.puid = userStore.getuserPms.uid ? userStore.getuserPms.uid : '';
    return await RoomList({ ...searchFormRef.value?.formModel, ...res });
  };

  // 重新加载表格数据
  function reloadTable() {
    actionRef.value?.reload();
  }

  // 拒绝提现申请
  function handleCancel(record: Recordable) {
    cancelRef.value.openModal(record);
  }

  //页面刷新  根据物业选择
  watch(
    () => userStore.getuserPms,
    () => {
      reloadTable();  //当前页面重新加载的数据
    },
    {
      immediate: true,
      deep: true,
    }
  );

  // 查看详情
  function handleView(record: Recordable) {
    record.viewtype = '住宿订单'
    orderViewRef.value.openModal(record);
  }

  onMounted(() => {
    loadOptions();
  });
</script>

<style lang="less" scoped></style>
